package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Member;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.util.ToastView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 自动匹配
 * Created by Administrator on 2017/10/13.
 */
public class AutomaticActivity extends Activity implements View.OnClickListener{

    private Context context;

    private TextView tv_coach_name,tv_coach_dan,tv_coach_heat;
    private ImageView iv_coach_img;

    private SendRequest send;

    //用户编辑器
    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    //预约时间,陪玩时长,
    private String playStartTime = "",hoursNum = "",userId = "";
    private Member member;

    private ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_layout_automatic);
        AutomaticActivity.this.setFinishOnTouchOutside(false);
        context = AutomaticActivity.this;

        playStartTime = getIntent().getStringExtra("playStartTime");
        hoursNum = getIntent().getStringExtra("hoursNum");

        initData();
        initView();
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.playStartTime = playStartTime;
            send.hoursNum = hoursNum;
            send.findType = "0";
            //预约
            nHttpClient.getAll(ApiInterface.FINDSUIMEMBER, send.tofindSuiMemberJson().toString(), indexHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private void initView() {
        iv_coach_img = (ImageView) findViewById(R.id.iv_coach_img);
        tv_coach_name = (TextView) findViewById(R.id.tv_coach_name);
        tv_coach_dan = (TextView) findViewById(R.id.tv_coach_dan);
        tv_coach_heat = (TextView) findViewById(R.id.tv_coach_heat);

        findViewById(R.id.tv_member_ok).setOnClickListener(this);
        findViewById(R.id.tv_member_replace).setOnClickListener(this);
    }

    private void setData(){
        tv_coach_name.setText(member.getMember_name());
        tv_coach_dan.setText("平台预约教练");
        tv_coach_heat.setText(member.getClick_num() + "");
        imageLoader.displayImage(ApiInterface.IMAGES + member.getMember_img(), iv_coach_img, App.options);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_member_ok:
                Intent intent = new Intent(context,SaveOrderActivity.class);
                intent.putExtra("playStartTime",playStartTime);
                intent.putExtra("hoursNum",hoursNum);
                intent.putExtra("member",member);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_member_replace:
                initData();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }

    private MyHttpClientRespenseHandler indexHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                try {
                    JSONObject subItemObject = json.getJSONObject("data").getJSONObject("members");
                    member = new Member();
                    member.setMember_id(subItemObject.optInt("member_id", 0));
                    member.setClick_num(subItemObject.optInt("click_num", 0));
                    member.setMember_img(subItemObject.optString("member_img"));
                    member.setMember_name(subItemObject.optString("member_name"));
                    member.setEvaluateInfo(subItemObject.optString("self_evaluation"));
                    setData();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                Toast.makeText(context, error_desc, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("picker_left", "error");
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        }

        @Override
        public void faile(String json) {
            Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
        }
    };
}
